package com.ocn.li.scmdy.modules.app.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.horizon.workflow.webservice.client.operatorwork.WorkBean;
import com.ocn.li.scmdy.common.utils.Constant;
import com.ocn.li.scmdy.common.utils.R;
import com.ocn.li.scmdy.common.utils.RedisUtils;
import com.ocn.li.scmdy.config.HorizonConfig;
import com.ocn.li.scmdy.modules.app.entity.Accounts;
import com.ocn.li.scmdy.modules.app.entity.DyUser;
import com.ocn.li.scmdy.modules.app.service.DyUserService;
import com.ocn.li.scmdy.modules.app.service.LoginService;
import com.ocn.li.scmdy.modules.app.utils.OperatorWorkUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName ApiFlowController
 * @Description TODO
 * @Author Li9527
 * @Date 2020/9/16 17:35
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/flow/hz")
@Api(value = "ApiFlowController", tags = {"移动端--慧正工作流引入demo"})
public class ApiFlowController {

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    LoginService loginService;

    @Autowired
    DyUserService userService;

    @Autowired
    HorizonConfig horizonConfig;

    private static final String flowId = "dongyingeventreportprocess";

/*    @Autowired
    private IWfApplyservice wfApplyservice;

    *//***
     * @Title: getEventClassifyByAccount_hz
     * @Description: <获取事项信息>
     * @Param: [accountId, response]
     * @Return: void
     * @Author: fjq
     * @Date: 18/6/22
     * @Modified By: fjq
     * @Modified Date: 18/6/22
     * @Why: <描述修改原因>
     * @Throws:
     *//*
    @GetMapping("getEventClassifyByAccount_hz")
    public R getEventClassifyByAccount_hz(@RequestParam(name = "账号id", required = true)  String accountId, HttpServletResponse response) {
        String json = wfApplyservicesImpl.getFlowList(accountId);
        return  R.ok("获取成功").put("data",json);
    }*/


    /**
     * @Author Li9527
     * @Description  客户端发起事件demo
     * @Date 2020/9/18 11:15 
     * @Param [accountId]
     * @return com.ocn.li.scmdy.common.utils.R
     **/
    @PostMapping("/addEventHz")
    @ApiOperation("事件发起")
    public R addEvent_HZ(@RequestParam(name = "accountId") String accountId) {
        try {
            // 账号信息
            Accounts account = loginService.selectOne(new EntityWrapper<Accounts>().eq(
                    "ID_", accountId).eq("del_Flag", Constant.BasicStatus.NORMAL.getValue()));
            // 工作流配置
            OperatorWorkUtils o = new OperatorWorkUtils(horizonConfig.getFlowurl());
            // user信息
            DyUser dyUser = userService.selectOne(new EntityWrapper<DyUser>().eq("ACCOUNT_ID", account.getId()));
            // 发起api
            WorkBean wb = o.create(dyUser.getAccountId(), flowId);
            // 返回比对错误码
            if (null != wb && (wb.getResult() == Constant.CreateFlowStatus.Init_Success.getValue() || wb.getResult() == 0)) {
                if (wb.getResult() == Constant.CreateFlowStatus.Init_Success.getValue()) {
                    return R.ok("开始进行慧正业务流组装").put("data", wb.getCurFlowInfo());
                } else {
                    return R.error("还没有进行业务组装，请检查");
                }
            } else {
                return R.error("打开流程实例失败");

            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("后台操作异常:" + e.getMessage());
        }
    }

    public static void main(String[] args) {
        // 写法1
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("我是写法1");
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();

        // 写法2
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("我是写法2");
            }
        }).start();

        // 写法3-lambda
        new Thread(() -> System.out.println("我是写法3")).start();






    }


}
